<script setup lang="ts">
import NoDataVue from "@/components/NoData/NoData.vue";
import {ElMessage} from "element-plus";
import PersonalResumeCard from "@/components/common/PersonalResumeCard.vue";
import {deleteResume, getResumeList} from "@/http/m_api/template";
import { ref } from 'vue';
import {formatDate} from "@/assets/ts/utils";
import appStore from "@/store";

const legoCreateList = ref<Array<any>>([]);
const page = ref<number>(1);
const limit = ref<number>(6);
const total = ref<number>(0);
const currentPage = ref<number>(1);
const isShowSkeleton = ref<boolean>(true);
const getUserTemplateList = async () => {
  const data = await getResumeList(page.value, limit.value);
  if (data.code === 0) {
    legoCreateList.value = data.data.list;
    legoCreateList.value.map((item: any) => {
      item.previewUrl = item.templateCover;
    });
    total.value = data.data.page.count;
    currentPage.value = data.data.page.currentPage;
    isShowSkeleton.value = false;
  }
  else {
    isShowSkeleton.value = false;
    ElMessage.error(data.data.message);
  }
};
getUserTemplateList();

// 改变页码时
const handleCurrentChange = (currentPage: number) => {
  page.value = currentPage;
  getUserTemplateList();
};

const toDesign = (item: any) => {
  // Placeholder for the toDesign function
};
const router = useRouter()

const deleteUserCreate = (item: any) => {
  deleteResume(item).then(()=>{
    ElMessage.success('删除成功')
    router.go(0)
  })
};
</script>

<template>
  <div class="resume-list-container">
    <!-- Header Section -->
    <div class="page-header">
      <div class="header-content">
        <h2 class="page-title">
          <i class="fas fa-file-alt"></i>
          我的简历
        </h2>
        <p class="page-subtitle">管理您创建的所有简历模板</p>
      </div>
      <div class="header-actions">
        <el-button type="primary" class="create-btn" @click="$router.push('/resume')">
          <i class="fas fa-plus"></i>
          创建新简历
        </el-button>
      </div>
    </div>

    <!-- Resume Content -->
    <div class="resume-content">
      <!-- Loading Skeleton -->
      <div v-if="isShowSkeleton" class="skeleton-container">
        <div class="skeleton-flex">
          <div class="skeleton-item" v-for="index in 6" :key="index">
            <div class="skeleton-card">
              <div class="skeleton-image"></div>
            </div>
            <div class="skeleton-title"></div>
            <div class="skeleton-date"></div>
          </div>
        </div>
      </div>

      <!-- Resume List -->
      <div v-else-if="legoCreateList.length" class="resume-flex">
        <div v-for="(item, index) in legoCreateList" :key="index" class="resume-item">
          <personal-resume-card
              :card-data="item"
              type="new"
              class="resume-card"
              @to-design="toDesign"
              @delete="deleteUserCreate"
          />
          <div class="resume-title">{{ item.title || '简历标题' }}</div>
          <div class="resume-date">{{ formatDate(new Date(item.createDate)) }}</div>
        </div>
      </div>

      <!-- Empty State -->
      <div v-else class="empty-state">
        <div class="empty-icon">
          <i class="fas fa-file-alt"></i>
        </div>
        <h3 class="empty-title">暂无简历</h3>
        <p class="empty-description">您还没有创建任何简历模板</p>
        <el-button type="primary" class="empty-action">
          <i class="fas fa-plus"></i>
          创建第一份简历
        </el-button>
      </div>

      <!-- Pagination -->
      <div v-if="!isShowSkeleton && legoCreateList.length" class="pagination-container">
        <el-pagination
            background
            :total="total"
            :page-size="limit"
            :page-sizes="[6, 12, 18, 24]"
            :current-page="currentPage"
            @current-change="handleCurrentChange"
            layout="total, sizes, prev, pager, next, jumper"
            class="custom-pagination"
        />
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
@import 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css';

.resume-list-container {
  max-width: 900px;
  margin: 0 auto;
  padding: 0;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  padding: 25px 30px;
  background: linear-gradient(135deg, rgba(100, 181, 246, 0.1), rgba(144, 202, 249, 0.05));
  border-radius: 16px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);

  .header-content {
    .page-title {
      color: #ffffff;
      font-size: 1.8rem;
      font-weight: 600;
      margin: 0 0 8px 0;
      display: flex;
      align-items: center;
      gap: 12px;

      i {
        color: #64b5f6;
        font-size: 1.6rem;
      }
    }

    .page-subtitle {
      color: #bdc3c7;
      font-size: 0.95rem;
      margin: 0;
    }
  }

  .header-actions {
    .create-btn {
      background: linear-gradient(135deg, #64b5f6, #42a5f5);
      border: none;
      border-radius: 25px;
      padding: 12px 24px;
      font-weight: 500;
      transition: all 0.3s ease;

      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 8px 25px rgba(100, 181, 246, 0.3);
      }

      i {
        margin-right: 8px;
      }
    }
  }
}

.resume-content {
  background: rgba(52, 73, 94, 0.6);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 16px;
  overflow: hidden;
  transition: all 0.3s ease;
  animation: slideInUp 0.5s ease-out;
  padding: 30px;

  &:hover {
    border-color: rgba(255, 255, 255, 0.2);
    box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
  }
}

.resume-flex {
  display: flex;
  flex-wrap: wrap;
  gap: 25px;
  justify-content: flex-start;
  max-height: 600px;
  overflow-y: auto;
  padding-right: 10px;
  margin-bottom: 30px;

  &::-webkit-scrollbar {
    width: 8px;
  }

  &::-webkit-scrollbar-track {
    background: rgba(255, 255, 255, 0.05);
    border-radius: 4px;
  }

  &::-webkit-scrollbar-thumb {
    background: linear-gradient(135deg, #64b5f6, #90caf9);
    border-radius: 4px;
  }

  &::-webkit-scrollbar-thumb:hover {
    background: linear-gradient(135deg, #42a5f5, #64b5f6);
  }
}

.resume-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 200px;
  transition: all 0.3s ease;
  padding: 15px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid rgba(255, 255, 255, 0.05);

  &:hover {
    transform: translateY(-5px);
    background: rgba(255, 255, 255, 0.05);
    border-color: rgba(100, 181, 246, 0.3);
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);

    .resume-card {
      transform: scale(1.02);
    }
  }
}

.resume-card {
  transition: all 0.3s ease;
  margin-bottom: 15px;
}

.resume-title {
  color: #ffffff;
  font-size: 1rem;
  font-weight: 600;
  text-align: center;
  margin-bottom: 8px;
  line-height: 1.4;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.resume-date {
  color: #bdc3c7;
  font-size: 0.85rem;
  text-align: center;
  display: flex;
  align-items: center;
  gap: 6px;

  &::before {
    content: '\f017';
    font-family: 'Font Awesome 6 Free';
    font-weight: 900;
    color: #64b5f6;
    font-size: 0.8rem;
  }
}

// Skeleton Loading
.skeleton-container {
  padding: 0;
}

.skeleton-flex {
  display: flex;
  flex-wrap: wrap;
  gap: 25px;
  justify-content: flex-start;
  margin-bottom: 30px;
}

.skeleton-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 200px;
  padding: 15px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid rgba(255, 255, 255, 0.05);
}

.skeleton-card {
  width: 160px;
  height: 220px;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 8px;
  margin-bottom: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.skeleton-image {
  width: 140px;
  height: 200px;
  background: linear-gradient(90deg, rgba(255, 255, 255, 0.05), rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0.05));
  border-radius: 6px;
  animation: shimmer 1.5s infinite;
}

.skeleton-title {
  height: 18px;
  width: 120px;
  background: linear-gradient(90deg, rgba(255, 255, 255, 0.05), rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0.05));
  border-radius: 4px;
  margin-bottom: 8px;
  animation: shimmer 1.5s infinite;
  animation-delay: 0.2s;
}

.skeleton-date {
  height: 14px;
  width: 80px;
  background: linear-gradient(90deg, rgba(255, 255, 255, 0.05), rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0.05));
  border-radius: 4px;
  animation: shimmer 1.5s infinite;
  animation-delay: 0.4s;
}

@keyframes shimmer {
  0% {
    background-position: -200px 0;
  }
  100% {
    background-position: 200px 0;
  }
}

// Empty State
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 30px;
  text-align: center;
  animation: fadeIn 0.5s ease-out;

  .empty-icon {
    font-size: 4rem;
    color: rgba(100, 181, 246, 0.3);
    margin-bottom: 20px;

    i {
      animation: float 3s ease-in-out infinite;
    }
  }

  .empty-title {
    color: #ffffff;
    font-size: 1.5rem;
    font-weight: 600;
    margin: 0 0 10px 0;
  }

  .empty-description {
    color: #bdc3c7;
    font-size: 1rem;
    max-width: 400px;
    margin: 0 0 25px 0;
  }

  .empty-action {
    background: linear-gradient(135deg, #64b5f6, #42a5f5);
    border: none;
    border-radius: 25px;
    padding: 12px 30px;
    font-weight: 500;
    transition: all 0.3s ease;

    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 8px 25px rgba(100, 181, 246, 0.3);
    }

    i {
      margin-right: 8px;
    }
  }
}

@keyframes float {
  0% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-10px);
  }
  100% {
    transform: translateY(0);
  }
}

// Pagination
.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px solid rgba(255, 255, 255, 0.05);
}

.custom-pagination {
  :deep(.el-pagination__total),
  :deep(.el-pagination__sizes),
  :deep(.el-pagination__jump) {
    color: #f5f5f5;
    margin-right: 15px;

    .el-input__inner {
      color: #f5f5f5;
      background: rgba(255, 255, 255, 0.1);
      border: 1px solid rgba(255, 255, 255, 0.2);
    }
  }

  :deep(.btn-prev),
  :deep(.btn-next),
  :deep(.el-pager li) {
    background: rgba(52, 73, 94, 0.8);
    color: #f5f5f5;
    border: 1px solid rgba(255, 255, 255, 0.1);

    &:hover {
      color: #64b5f6;
      background: rgba(100, 181, 246, 0.1);
    }

    &.is-active {
      background: linear-gradient(135deg, #64b5f6, #42a5f5);
      color: white;
    }
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes slideInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

// Responsive Design
@media (max-width: 768px) {
  .resume-list-container {
    padding: 0 15px;
  }

  .page-header {
    flex-direction: column;
    gap: 20px;
    text-align: center;

    .header-actions {
      width: 100%;
      display: flex;
      justify-content: center;
    }
  }

  .resume-flex,
  .skeleton-flex {
    justify-content: center;
    gap: 20px;
  }

  .resume-item,
  .skeleton-item {
    width: 180px;
  }

  .pagination-container {
    flex-direction: column;
    align-items: center;
    gap: 15px;
  }
}

@media (max-width: 480px) {
  .resume-item,
  .skeleton-item {
    width: 160px;
  }

  .resume-title {
    font-size: 0.9rem;
  }

  .resume-date {
    font-size: 0.8rem;
  }
}
</style>